﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using Metamorph.Core.Bpm.Events;
using System.Collections.Generic;
using Metamorph.Core.Bpm.Connectors;
using Metamorph.Core.Bpm.Activities;

namespace Metamorph.Core.Bpm
{
	public class ExecutionContext
	{
		protected List<IEvent>		lEvents;
		protected ISubprocess		sSubprocess;
		protected int				iCurrentIteration;
		protected IBusinessProcess	bpBusinessProcess;
		protected IShape			sCurrentShape, sOwnerShape;

		public ExecutionContext(IShape sOwnerShape, IBusinessProcess bpBusinessProcess)
		{
			this.sOwnerShape		= sOwnerShape;
			this.bpBusinessProcess	= bpBusinessProcess;
		}

		public ExecutionContext(IShape sOwnerShape, IBusinessProcess bpBusinessProcess, ISubprocess sSubprocess)
		{
			this.sSubprocess		= sSubprocess;
			this.sOwnerShape		= sOwnerShape;
			this.bpBusinessProcess	= bpBusinessProcess;
			//CurrentShape			= sSubprocess.InitialShape;
		}

		public ExecutionContext(IShape sOwnerShape, IBusinessProcess bpBusinessProcess, int iCurrentIteration)
		{
			this.sOwnerShape		= sOwnerShape;
			this.iCurrentIteration	= iCurrentIteration;
			this.bpBusinessProcess	= bpBusinessProcess;
		}

		public ExecutionContext(IShape sOwnerShape, IBusinessProcess bpBusinessProcess, ISubprocess sSubprocess, int iCurrentIteration)
			: this(sOwnerShape, bpBusinessProcess, sSubprocess)
		{
			this.iCurrentIteration = iCurrentIteration;
		}

		public ExecutionContext(IShape sOwnerShape, IBusinessProcess bpBusinessProcess, ITask tTask)
		{
			this.sOwnerShape		= sOwnerShape;
			this.bpBusinessProcess	= bpBusinessProcess;

			sCurrentShape = tTask;
		}

		public ExecutionContext(IShape sOwnerShape, IBusinessProcess bpBusinessProcess, ITask tTask, int iCurrentIteration)
		{
			this.sOwnerShape		= sOwnerShape;
			this.iCurrentIteration	= iCurrentIteration;
			this.bpBusinessProcess	= bpBusinessProcess;

			sCurrentShape			= tTask;
		}

		public IShape InitialShape
		{
			set
			{
				sCurrentShape = value;
			}
		}

		public IShape CurrentShape
		{
			get
			{
				return sCurrentShape;
			}
			set
			{
				if (sCurrentShape != null)
				{
					sCurrentShape.Uninit(bpBusinessProcess);
				}

				sCurrentShape = value;

				if (sCurrentShape != null)
				{
					sCurrentShape.Init(bpBusinessProcess);
				}
			}
		}

		public IShape OwnerShape
		{
			get
			{
				return sOwnerShape;
			}
			set
			{
				sOwnerShape = value;
			}
		}

		public List<IEvent> Events
		{
			get
			{
				return lEvents;
			}
			set
			{
				lEvents = value;
			}
		}

		public int CurrentIteration
		{
			get
			{
				return iCurrentIteration;
			}
			set
			{
				iCurrentIteration = value;
			}
		}

		public ISubprocess Subprocess
		{
			get
			{
				return sSubprocess;
			}
			set
			{
				sSubprocess = value;

				CurrentShape = sSubprocess.InitialShape;
			}
		}
	}
}
